package net.mengkang;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.time.LocalDateTime;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;


import data.basicData;
import data.messageDispath;

//处理文本协议数据，处理TextWebSocketFrame类型的数据，websocket专门处理文本的frame就是TextWebSocketFrame
public class TextWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame>{

    //读到客户端的内容并且向客户端去写内容
 

   
	//每个channel都有一个唯一的id值
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        //打印出channel唯一值，asLongText方法是channel的id的全名
        System.out.println("handlerAdded："+ctx.channel().id().asLongText());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    	
    	  
        System.out.println("handlerRemoved：" + ctx.channel().id().asLongText());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("异常发生");
        ctx.close();
    }
    @Override
    protected void   messageReceived(ChannelHandlerContext ctx, TextWebSocketFrame msg) 
    {
         
    	String str=msg.text();
    	
    	basicData newPerson = JSON.parseObject(msg.text(), basicData.class);
    	messageDispath.getInstance().MessageDispath(newPerson.getMsgId(),str,ctx);
     

     //   ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(newPerson)));
    }


}

